pub use ratatui::{
    backend::CrosstermBackend,
    crossterm::{
        event::{self, Event, KeyCode},
        terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
        ExecutableCommand,
    },
    layout::{Constraint, Layout},
    prelude::*,
    widgets::*,
    CompletedFrame, DefaultTerminal, Frame, Terminal,
};
use std::io::{self, stdout};
use tool::tool_result::Error;

pub struct TuiHelper {}

impl TuiHelper {
    pub fn init() -> Result<DefaultTerminal, Error> {
        //启动备用屏幕和启用原始模式
        match enable_raw_mode() {
            Ok(_) => {}
            Err(e) => return Err(Error::new(e.to_string())),
        };
        let mut stdout = stdout();
        match stdout.execute(EnterAlternateScreen) {
            Ok(_) => {}
            Err(e) => return Err(Error::new(e.to_string())),
        }; 
        let terminal = ratatui::init();
        Ok(terminal)
    }
    pub fn shut_down() {
        ratatui::restore();
        std::process::exit(0);
    }
    pub fn shut_down_msg<T: Into<String>>(msg: T) {
        ratatui::restore();
        println!("{}", msg.into());
        std::process::exit(0);
    }
}
